package com.kwok.rbac.application.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kwok.rbac.application.common.utils.R;
import com.kwok.rbac.application.entity.Role;
import com.kwok.rbac.application.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 角色表 前端控制器
 * </p>
 *
 * @author baomidou
 * @since 2024-05-25
 */
@RestController
@RequestMapping("/role")
public class RoleController {

    @Autowired
    private RoleService roleService;

    //添加角色
    @PostMapping("/save")
    public R addRole(@RequestBody Role role){
        return R.write(roleService.save(role));
    }

    //批量添加角色
    @PostMapping("/batchSave")
    public R batchAddRole(@RequestBody List<Role> roleList){
        return R.write(roleService.saveBatch(roleList));
    }

    //根据主键查询单个角色
    @GetMapping("/get/{roleId}")
    public R getRoleById(@PathVariable Integer roleId){
        return R.success(roleService.getById(roleId));
    }

    //查询所有角色
    @GetMapping("/list")
    public R getAllRole(){
        return R.success(roleService.list());
    }

    //分页查询角色
    @PostMapping("/rolePage")
    public Page<Role> getRolePage(@RequestBody Page<Role> page){
        Page<Role> rolePage = roleService.page(page);
        return rolePage;
    }

    //根据主键修改角色信息
    @PutMapping("/update")
    public R updateRole(@RequestBody Role role){
        return R.write(roleService.updateById(role));
    }

    //根据主键删除角色
    @DeleteMapping("/{roleId}")
    public R removeRole(@PathVariable Integer roleId){
        return R.write(roleService.removeById(roleId));
    }

    //根据主键批量删除角色
    @DeleteMapping
    public R batchRemoveRole(@RequestBody List<Integer> roleIdList){
        return R.write(roleService.removeByIds(roleIdList));
    }

    //给用户赋予角色(角色的添加)
    @PostMapping("/addRoleToUser/{userId}")
    public R addRoleToUser(@PathVariable Integer userId, @RequestBody List<Integer> roleIdList){
        Boolean b = roleService.addRoleToUser(userId, roleIdList);
        return R.write(b);
    }

    //查询用户拥有的角色（角色的查询）
    @GetMapping("/getRoleListByUserId/{userId}")
    public R getRoleListByUserId(@PathVariable Integer userId){
        return R.success(roleService.getRoleListByUserId(userId));
    }

}
